package com.css.bjdt_report.validation.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.css.bjdt_report.cell.entity.Cell;
import com.css.bjdt_report.cell.entity.Cell.CellType;
import com.css.bjdt_report.cell.service.CellDataService;
import com.css.bjdt_report.cell.service.CellService;
import com.css.bjdt_report.executive.report.ReportRequest;
import com.css.bjdt_report.executive.util.ExpParser;
import com.css.bjdt_report.executive.report.ReportCache;
import com.css.bjdt_report.executive.entity.Exp;
import com.css.bjdt_report.report.entity.RptTemplateUnitRela;
import com.css.bjdt_report.report.service.ReportTemplateManageService;
import com.css.bjdt_report.report.service.ReportTemplateUnitRelaService;
import com.css.bjdt_report.report.service.ReportTemplateValidationService;
import com.css.bjdt_report.validation.ReportDataValidator;
import com.css.bjdt_report.validation.ValidationExpParser;
/**
 * 
 * @author qiushi
 *
 */
@Service
public class ReportValidateService {
	@Autowired
	private ReportTemplateManageService reportTemplateManageService;
	@Autowired
	private ReportTemplateUnitRelaService reportTemplateUnitRelaService;
	@Autowired
	private ReportTemplateValidationService reportTemplateValidationService;
	@Autowired
	private CellService cellService;
	@Autowired
	private CellDataService cellDataService;
	@Autowired
	private ValidationInfoService validationInfoService;
	
	private ExpParser parser = ValidationExpParser.getInstance();
	private ReportDataValidator validator = ReportDataValidator.getInstance();
	private ReportCache reportCache = ReportCache.getInstance();
	
	private static Log logger = LogFactory.getLog(ReportValidateService.class);
	/**
	 * 校核报表数据
	 * @param annualId 年度id
	 * @param templateId 报表id
	 * @param unitId 党委id
	 */
	public void checkReportData(ReportRequest request){
		String annualId = request.getAnnualId();
		String[] templateIds = request.getTemplateIds().toArray(new String[0]);
		long time1 = System.currentTimeMillis();
		init(annualId);
		long time2 = System.currentTimeMillis();
		logger.debug("init cost:"+(time2-time1));
		initValidationRuleByTemplate(templateIds);
		long time3 = System.currentTimeMillis();
		logger.debug("initValidationRuleByTemplate cost:"+(time3-time2));
		validateIndicatorData(request);
		long time4 = System.currentTimeMillis();
		logger.debug("validateIndicatorData cost:"+(time4-time3));
		destory();
	}
	/**
	 * 初始化
	 * @param annualId
	 */
	private void init(String annualId){
		reportCache.clear();
		validator.setCellDataSvc(cellDataService);
		validator.setValidationInfoSvc(validationInfoService);
		validator.setValidationSvc(reportTemplateValidationService);
		validator.isExistTable(annualId);
	}
	/**
	 * 按报表初始化校核规则
	 * @param templateIds
	 */
	private void initValidationRuleByTemplate(String[] templateIds){
		for (String templateId : templateIds) {
			initValidationRuleByTemplate(templateId);
		}		
	}
	/**
	 * 按报表初始化校核规则
	 * @param templateId
	 */
	private void initValidationRuleByTemplate(String templateId){
		List<Cell> cells = new ArrayList<Cell>();
		List<Cell> inputCells = cellService.getCellsByTemplateIdAndType(templateId, CellType.input);
		List<Cell> statCells = cellService.getCellsByTemplateIdAndType(templateId, CellType.stat);
		List<Cell> calcCells = cellService.getCellsByTemplateIdAndType(templateId, CellType.calc);
		cells.addAll(inputCells);
		cells.addAll(statCells);
		cells.addAll(calcCells);		
		Map<String,List<Exp>> validationMap = reportTemplateValidationService.buildReportValidationMapByTemplate(templateId);
		parser.setExpMap(validationMap);
		for (Cell cell : cells) {
			parser.parseExp(cell);			
		}
		List<Cell> validationcells = new ArrayList<Cell>();
		for (Cell cell : cells) {
			if(cell.getValidationExps() != null && cell.getValidationExps().size() > 0){
				validationcells.add(cell);
			}
		}
		reportCache.putContent(templateId, CellType.vali, validationcells);
	}
	/**
	 * 校核指标数据
	 * @param annualId
	 * @param templateId
	 * @param unitIds
	 */
	private void validateIndicatorData(ReportRequest request){
		List<String> dzzIds = request.getDzzIds();
		List<RptTemplateUnitRela> templateUnitRelas = reportTemplateUnitRelaService.getTemplateUnitRela(request);
		for (String dzzId : dzzIds) {
			List<RptTemplateUnitRela> newtemplateUnitRelas = reportTemplateUnitRelaService.cloneTemplateUnitRela(dzzId, templateUnitRelas);
			validator.validate(newtemplateUnitRelas);
		}
	}
	
	private void destory(){
		parser.destory();
		validator.destory();
	}
}
